#!/bin/bash
#
# Copyright (c) Citrix Systems 2008. All rights reserved.
#
#     ./test-pool-size n
#
# Host1 will become the master of the pool, with host2 ... hostN as slaves.
# Then, on each host, vm_per_host VMs are created, with names debian-etch-HOST_NAME-i (for i in 1..vm_per_host)

if [ $# -ne 1 ]; then 
   echo "Usage: $0 number_of_vm"
   echo "Need :"
   echo "  * ./repeat, ./repeat-clone, ./repeat-start and ./repeat-destroy scripts to be in the same directory that ${0};"
   echo "  * a pool already set up with a shared NFS storage and a HVM VM called dsl;"
   echo "  * ${0} must be started on the master of this pool;"
   echo "${0} clones <number_of_vm>, then starts them all, then shutdown them all, then destroy them all. Then it ejects one host of the pool, and do the same tests again until the master remains  the last host in the pool. Each operation is recoreded into a .dat file."
   exit 1
fi 

N=${1}
IFS=:','
HOSTS=`xe host-list --minimal`
MASTER=`xe pool-list params=master --minimal`

c=`xe host-list --minimal | sed -e 's/,/\n/g' | wc -l`


#main loop
for HOST in $HOSTS; 
do 
    if [ ${HOST} != ${MASTER} ]; then
	./repeat-clone ${N} dsl > clone-${c}.dat
	./repeat-start ${N} dsl > start-${c}.dat
	./repeat ${N} shutdown dsl --force  > shutdown-${c}.dat
	./repeat-destroy ${N} dsl > destroy-${c}.dat
	
	echo "Ejecting ${HOST}."
	xe pool-eject host-uuid=${HOST} --force
	#xe host-forget uuid=${HOST}
	((c--))
	echo "Ejected."
    fi
done
